package cn.tedu.bookstore.controller;

import cn.tedu.bookstore.mapper.UserMapper;
import cn.tedu.bookstore.pojo.dto.UserRegDTO;
import cn.tedu.bookstore.pojo.vo.UserRegVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
//@RequestMapping("/user")
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;
    @RequestMapping("/user/reg")
    public int reg(@RequestBody UserRegDTO user){
        System.out.println("user = " + user);
        UserRegVO u = mapper.selectByPhone(user.getPhone());
        String phone = user.getPhone();
        String u1 = user.getUsername();
        String regex = "[1][34578][0-9]{9}";
        if(phone.matches(regex)){
            System.out.println("格式正确");
            if(u!=null){
                return 3;   //电话号已存在
            }else {
                String u2 = mapper.selectByUsername(u1);
                if(u2!= null){
                    return 4;   //用户名已存在
                }
            }
            mapper.insert(user);
            return 1;   //注册成功
        }else {
            System.out.println("格式不正确");
            return 2;   //格式不正确
        }
    }
}
